package com.zzyl.controller.security;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/role")
@RequiredArgsConstructor
@Api(tags = "角色管理")
public class RoleController {

    private final RoleService roleService;

    @PostMapping(path = "/page/{pageNum}/{pageSize}")
    @ApiOperation("角色分页查询")
    public ResponseResult<PageResponse<RoleVo>> pageQuery(@PathVariable int pageNum, @PathVariable int pageSize,@RequestBody RoleDto roleDto){
        PageResponse<RoleVo> role = roleService.pageQuery(pageNum,pageSize,roleDto);
        return ResponseResult.success(role);
    }

    @PutMapping
    @ApiOperation("新增角色")
    public ResponseResult<Void> addRole(@RequestBody RoleDto roleDto){
        roleService.addRole(roleDto);
        return ResponseResult.success();
    }

    @GetMapping(path = "/find-checked-resources/{roleId}")
    @ApiOperation("查询角色选中的资源数据")
    public ResponseResult<List<String>> queryResourceByRoleId(@PathVariable Long roleId){
        List<String> resource= roleService.queryResourceByRoleId(roleId);
        return ResponseResult.success(resource);
    }

    @PatchMapping
    @ApiOperation("角色修改")
    public ResponseResult<Void> modifyRole(@RequestBody RoleDto roleDto){
        roleService.modifyRole(roleDto);
        return ResponseResult.success();
    }

    @DeleteMapping(path = "/{roleIds}")
    @ApiOperation("删除角色")
    public ResponseResult<Void> deleteRole(@PathVariable Long roleIds){
        roleService.deleteRole(roleIds);
        return ResponseResult.success();
    }

    @PostMapping(path = "/init-roles")
    @ApiOperation("角色下拉框")
    public ResponseResult<List<RoleVo>> initRoles(){
        List<RoleVo> list=roleService.initRoles();
        return ResponseResult.success(list);
    }
}
